package com.example.chatchatbackend.service.impl.sendJob;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.send.SendComments;
import com.example.chatchatbackend.entity.vo.request.SendCommentsVO;
import com.example.chatchatbackend.mapper.SendCommentsMapper;
import com.example.chatchatbackend.service.sendJob.SendCommentsService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class SendCommentsServiceImpl extends ServiceImpl<SendCommentsMapper, SendComments> implements SendCommentsService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private SendCommentsMapper sendCommentsMapper;
    @Override
    public List<SendCommentsVO> getAllSendComments() {
          return sendCommentsMapper.getAll();
    }

    @Override
    public SendCommentsVO insertSendComments(SendCommentsVO sendCommentsVO) {
        SendComments comment = new SendComments(null,sendCommentsVO.getProductId(),sendCommentsVO.getUserId(),sendCommentsVO.getComment(),this.currentTime,sendCommentsVO.getSendTime(),this.currentTime,sendCommentsVO.getStatus(),0);
        if(this.save(comment))
            return sendCommentsVO;
        return null;
    }

    @Override
    public List<SendCommentsVO> insertSendCommentsList(List<SendCommentsVO> sendCommentsVOList) {

        List<SendComments> sendCommentsList = new ArrayList<>();

        for (SendCommentsVO sendCommentsVO : sendCommentsVOList) {
            sendCommentsList.add(new SendComments(null,sendCommentsVO.getProductId(),sendCommentsVO.getUserId(),sendCommentsVO.getComment(),this.currentTime,sendCommentsVO.getSendTime(),this.currentTime,sendCommentsVO.getStatus(),0));
        }
        sendCommentsMapper.insertList(sendCommentsList);
        return sendCommentsVOList;
    }


    @Override
    public SendCommentsVO updateSendComments(SendCommentsVO sendCommentsVO) {
        SendComments sendComments = new SendComments();
        BeanUtils.copyProperties(sendCommentsVO, sendComments);
        sendComments.setUpdateTime(this.currentTime);
        sendCommentsMapper.updateSendComments(sendComments);
        return sendCommentsVO;
    }
    
    @Override
    public IPage<SendComments> pageThree(Integer pageNum, Integer pageSize) {
        Page<SendComments> page = new Page<>(pageNum, pageSize);
        IPage<SendComments> sendCommentsIPage = sendCommentsMapper.selectAll(page, null);
        return sendCommentsIPage;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        sendCommentsMapper.deleteBatchIds(ids);
    }

    @Override
    public List<SendCommentsVO> findTasksToSend(LocalDateTime currentTime) {


        List<SendComments> sendComments = sendCommentsMapper.findSendCommentsList("0",currentTime);

        List<SendCommentsVO> sendCommentsVOS = sendComments.stream()
                .map(sendComment -> {
                    SendCommentsVO send = new SendCommentsVO();
                    BeanUtils.copyProperties(sendComment, send);
                    return send;
                })
                .collect(Collectors.toList()); // 使用 collect(Collectors.toList())

        return sendCommentsVOS;
    }


}
